﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

using Microsoft.VSTalk.Integration;
using Microsoft.VSTalk.Integration.Indicator;

using VSTalk.Engine;
using VSTalk.Engine.Core.EnvironmentManagers.UI;
using VSTalk.Engine.Core.Feed.MessageState;
using VSTalk.Tools;

namespace Microsoft.VSTalk.Indicator
{
    public class IndicatorManager : IIndicatorManager, IDisposable
    {
        private AggregatedMessageCounter _counter;
        
        private Canvas _indicator;

        private ISubscription _counterUnit;

        public IMessageSateManager StateManager
        {
            get { return AppServices.Get<IMessageSateManager>(); }
        }

        public IWindowManager WindowManager
        {
            get { return AppServices.Get<IWindowManager>(); }
        }

        public void Initialize()
        {
            _counter = StateManager.AggregatedCounter;
            _counterUnit = _counter.SubscribeToChange(() => _counter.HasMessages, HandleCounter);
        }

        private void HandleCounter(AggregatedMessageCounter sender)
        {
            if (_counter.HasMessages)
            {
                var indicator = GetOrCreateIndicator();
                if (indicator == null) return;
                indicator.Visibility = Visibility.Visible;
            }
            else
            {
                if (_indicator == null) return;
                _indicator.Visibility = Visibility.Collapsed;
            }
        }

        private Canvas GetOrCreateIndicator()
        {
            if (_indicator != null)
            {
                return _indicator;
            }
            var bar = VisualHelper.FindStatusBar();
            if (bar == null)
            {
                //HandleMouseClick(null, null);
                return null;
            }
            _indicator = CreateIndicator();
            bar.Children.Insert(0, _indicator);
            return _indicator;
        }

        public Canvas CreateIndicator()
        {
            var canvas = (Canvas) XamlReader.Parse(@"
		<Canvas xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" x:Name=""appbar_email"" Clip=""F1 M 0,0L 76,0L 76,76L 0,76L 0,0"" Width=""20"" Height=""15"">
			<Path Fill=""White"" Data=""F1 M 21.5,23L 54.5,23C 56.2489,23 58,24.7511 58,26.5L 58,49.5C 58,51.2489 56.2489,53 54.5,53L 21.5,53C 19.7511,53 18,51.2489 18,49.5L 18,26.5C 18,24.7511 19.7511,23 21.5,23 Z M 22,47C 22,48.3117 22.6883,49 24,49L 52,49C 53.3117,49 54,48.3117 54,47L 54,29L 40.6563,42.6567C 39.4197,43.8933 37.4146,43.8933 36.178,42.6567L 22,29L 22,47 Z M 25,27L 36.7378,38.7381C 37.6653,39.6656 39.169,39.6656 40.0965,38.7381L 52,27L 25,27 Z "" Width=""20"" Height=""15"" HorizontalAlignment=""Left"" VerticalAlignment=""Top"" Canvas.Left=""0"" Canvas.Top=""0"" Stretch=""Fill""/>
		</Canvas>
");
            canvas.Cursor = Cursors.Hand;
            canvas.Background = Brushes.Transparent;
            canvas.MouseDown += HandleMouseClick;

            return canvas;
        }

        private void HandleMouseClick(object sender, MouseButtonEventArgs e)
        {
            var interlocutors = StateManager.GetWaitingInterlocutors();
            if (!interlocutors.Any()) return;
            WindowManager.ShowChatWindow(interlocutors.First());
        }

        public void Dispose()
        {
            if (_indicator != null)
            {
                _indicator.MouseDown -= HandleMouseClick;
            }
            _counterUnit.Unsubscribe();
        }
    }
}